package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Yan Dexuan
 * @since 2022/07/09 10:17
 */
@RestController
public class ShoppingItemController {

    @Autowired
    ICartService iCartService;

    /**
     * 删除购物车中选中的商品
     * @param userId
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author Yan Dexuan
     * @since 2022/07/09 15:04
     */

    @DeleteMapping("/shopping/items/{uid}")
    public ResponseData deleteItems(@PathVariable("uid") Long userId){

        //创建request对象。
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(userId);

        DeleteCheckedItemResposne resposne = iCartService.deleteCheckedItem(request);

        if (resposne.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(ShoppingRetCode.SUCCESS.getMessage());
        }

        return new ResponseUtil().setErrorMsg(resposne.getMsg());

    }

    /**
     * 删除购物车中的商品
     * @param userId
     * @param productId
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author Yan Dexuan
     * @since 2022/07/10 16:53
     */

    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteSingle(@PathVariable("uid") Long userId,
                                     @PathVariable("pid") Long productId){

        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(userId);
        request.setItemId(productId);

        DeleteCartItemResponse response = iCartService.deleteCartItem(request);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(ShoppingRetCode.SUCCESS.getMessage());
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

    /**
     * 全选或者不全选购物车中的商品
     * @param request
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author Yan Dexuan
     * @since 2022/07/10 16:53
     */
    @PutMapping("/shopping/items")
    public ResponseData chooseAll(@RequestBody CheckAllItemRequest request){

        CheckAllItemResponse response = iCartService.checkAllCartItem(request);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(ShoppingRetCode.SUCCESS.getMessage());
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

}
